<?php
function getDataReportContactStatus() {
	//include ('includes/classes/jpgraph/jpgraph.php');
	//include ('includes/classes/jpgraph/jpgraph_bar.php');
	//include ('includes/classes/jpgraph/jpgraph_pie.php');
	include('includes/openFlashCharts/open-flash-chart.php');
	$g = new graph();
	
	global $dbname, $db, $db_selected, $colorArray;
	
	$sql = "SELECT ContactID AS TotalContacts from cdc_Contacts WHERE OrgFlag = '".$_SESSION['org']."'";
	$result = mysql_db_query($dbname,$sql);
	$totalRows = mysql_num_rows($result);
	
	$sql = "SELECT COUNT(ContactID) AS Number,Status from cdc_Contacts WHERE OrgFlag = '".$_SESSION['org']."' GROUP BY Status";
	$result = mysql_db_query($dbname,$sql);
	
	$chartTitle = 'Status of all contacts ('.$totalRows.' total contacts)';
	$stringJSON   = '({"DataSets":"1","ChartTitle":"'.$chartTitle.'","ChartType":"p","ChartSize":"450x180","DataPoints":[';
		
	$a=0;
	while ($value = mysql_fetch_assoc($result)) {
		switch ($value['Status']) {
			case 1:
				$statusText = 'In progress';
				break;
			case 2:
				$statusText = 'Sent for follow-up';
				break;
			case 3:
				$statusText = 'Closed';
				break;	
			case 4:
				$statusText = 'Reopened';
				break;	
			case 5:
				$statusText = 'Closure Overdue!';
				break;			
		}
		
		$percent = floor(($value['Number']/$totalRows)*100);
		$label = $statusText.' ('.$value['Number'].')';

		$phpChartLabelArray[$a] = $label;
		$phpChartDataArray[$a]  = $percent;  
		
		$newArray = array(
						'Data1'  => $percent,
						'Label1' => $label
					);
		$stringJSON .= Zend_Json::encode($newArray).',';
		$a++;
	}	
	$stringJSON = substr($stringJSON,0,-1);
	$stringJSON .= ']})';
	
	/*
	 * This is for Google Charts
	$graph = new GphpChart('p');
	$graph->title = $chartTitle;
	$graph->add_data($phpChartDataArray,'003399');
	$graph->add_labels('x',$phpChartLabelArray);
	$graph->width = 450;
	$graph->height = 160;
	$graphImage = $graph->get_Image_String();
	*/

	/*
	 * This is for JPGraph
	$graph  = new PieGraph (400,350);
	$graph->SetFrame(false); 
	$graph->SetScale("textlin");
	$graph->img->SetImgFormat("jpeg");
	$graph->img->SetQuality(100); 
	$graph->SetAntiAliasing();
	$graph->title->Set($chartTitle);
	
	$p1 = new PiePlot($phpChartDataArray);
	$p1->SetLabels($phpChartLabelArray); 	
	//$p1->value->SetFont(FF_ARIAL,FS_NORMAL,8);
	$p1->SetSize(80); 
	$p1->ShowBorder();
	$p1->SetLabelPos(1.2); 
	
	$graph->Add($p1);
	$filePath = 'images/graphImages/graph.jpg';
	 
	$graph->Stroke($filePath);
	$graphHTML = "<img src=\"$filePath\" border=0>";
	*/

	/*
	 * This is for Open Flash Charts
	 */
	$g->pie(60,'#505050','{font-size: 12px; color: #404040;');
	//
	// pass in two arrays, one of data, the other data labels
	//
	$g->pie_values($phpChartDataArray,$phpChartLabelArray);
	$g->pie_slice_colours( array('#d01f3c','#356aa0','#C79810') );
	$g->set_tool_tip( '#val#%' );
	$g->bg_colour = '#FFFFFF';
	$g->title( $chartTitle, '{font-size:18px; color: #000; font-family: arial;}' );
	echo $g->render();
	

	//return $graphImage;
	//return $graphHTML;
}

function googleGraphAutoScale($max,$min=0,$inc) {
	/*
	 * This function takes a max value and no. of
	 * inc-rements and returns an array of nice
	 * values to use in a Google graph axis.
	 * Remember, 100 is max
	 */
	
}

function getDataReportCallsStaffMember() {
	global $dbname, $db, $db_selected;
	
	
	
	$sql = "SELECT ContactID AS TotalContacts from cdc_Contacts";
	$result = mysql_db_query($dbname,$sql);
	$totalRows = mysql_num_rows($result);
	
	$sql = "SELECT COUNT(ContactID) AS NumberOpened,C.UseridEntry,
			S.StaffFName,S.StaffLName
			FROM 
				cdc_Contacts C
			LEFT JOIN
				cdc_Staff S ON S.StaffID = C.UseridEntry
			GROUP BY C.UseridEntry";
	$result = mysql_db_query($dbname,$sql);
	
	$chartTitle = 'Contacts opened / closed by staff member|('.$totalRows.' total contacts)';
	$stringJSON   = '({"DataSets":"2","ChartTitle":"'.$chartTitle.'","ChartType":"bhg","ChartSize":"450x180","DataPoints":[';
	
	$a=0;
	while ($value = mysql_fetch_assoc($result)) {
		$staffFullName = $value['StaffFName'].' '.$value['StaffLName'];
		if (strlen($staffFullName)>15) {
			$staffFullName = substr($staffFullName,0,12).'...';
		}
		
		$labelArray[$a] = $staffFullName;
		$data1Array[$a] = $value['NumberOpened'];
		
		/*
		 * Did this staff member close any contacts?
		 */
		$sql2 = "SELECT COUNT(ContactID) AS NumberClosed 
				FROM cdc_Contacts WHERE UseridLastUpdate = ".$value['UseridEntry'];
		$result2 = mysql_db_query($dbname,$sql2);
		$row = mysql_fetch_assoc($result2);
		
		$data2Array[$a] = $row['NumberClosed'];
		
		$a++;
	}	
	$totalOpened = $a-1;
	
	/*
	 * We need to get the largest value of data1Array,
	 * divide by 10 and ceil to get x-axis values
	 */
	$largestValue = max($data1Array);
	$val = ceil($largestValue/5);
	for($a=0;$a<=5;$a++) {
		$xLabelArray[$a] = floor($val*($a)); 
	}	
	
	$stringJSON = substr($stringJSON,0,-1);
	$stringJSON .= ']})';
	
	$graph = new GphpChart('bhs');
	$graph->title = $chartTitle;
	$graph->add_data($data1Array,'AFD8F8');
  	$graph->add_data($data2Array,'253C87');
  	$graph->set_bar_width(18,8);
	$graph->add_labels('y',$labelArray);
	$graph->add_labels('x',$xLabelArray);
	$graph->width = 380;
	$graph->height = 380;
	$graph->prepare_data();
	$graph->fill('c','ls','0,FAF7F7,0.2,FEFEFE,0.2');
	$graph->add_grid('10,10,1,5');	
	$graphImage = $graph->get_Image_String();
	return $graphImage;
}

if ($_GET['reportType'] == 'status') {
	echo getDataReportContactStatus();
}
?>